keywords = {
    'const':'CONSTTK',
    'int':'INTTK',
    'char':'CHARTK',
    'void':'VOIDTK',
    'main':'MAINTK',
    'if':'IFTK',
    'else':'ELSETK',
    'do':'DOTK',
    'while':'WHILETK',
    'for':'FORTK',
    'scanf':'SCANFTK',
    'printf':'PRINTFTK',
    'return':'RETURNTK'
}

with open('output.txt', 'a+', encoding='utf-8') as fout:
    fout.seek(0), fout.truncate()
    with open('testfile.txt', 'r', encoding='utf-8') as fin:
        word = ''
        f = fin.readlines()
        # 遍历每行字符串
        for line in f:
            line = line.strip('\n')
            i = 0
            # 遍历单行字符串的每个字符
            while i < len(line):
                wordType: str
                # 忽略空格符
                while i<len(line) and line[i]==' ':
                    i += 1

                # 标识符判断
                if line[i].isalpha() or line[i] == '_':
                    string, word = '', line[i]
                    while i + 1 < len(line) and (line[i + 1].isalnum() or line[i + 1] == '_'):
                        word += line[i + 1]
                        i += 1
                    # 判断是否为关键字
                    if word in keywords.keys():
                        wordType = keywords[word]
                    else:
                        wordType = 'IDENFR'

                # 整形常量判断
                elif line[i].isdigit():
                    wordType = 'INTCON'
                    word = line[i]
                    while i+1 < len(line) and line[i+1].isdigit():
                        word += line[i+1]
                        i += 1

                # 判断算符
                else:
                    word = line[i]
                    if line[i] == '+':
                        wordType = 'PLUS'
                    elif line[i] == '-':
                        wordType = 'MINU'
                    elif line[i] == '*':
                        wordType = 'MULT'
                    elif line[i] == '/':
                        wordType = 'DIV'
                    # 判断是'='还是'=='
                    elif line[i] == '=':
                        if i + 1 < len(line) and line[i + 1] == '=':
                            i += 1
                            word += line[i]
                            wordType = 'EQL'
                        else:
                            wordType = 'ASSIGN'
                    # 判断是'!'还是'!='
                    elif line[i] == '!':
                        if i + 1 < len(line) and line[i + 1] == '=':
                            i += 1
                            word += line[i]
                            wordType = 'NEQ'
                        else:
                            pass
                    # 判断是'<'还是'<='
                    elif line[i] == '<':
                        if i+1<len(line) and line[i+1]=='=':
                            i += 1
                            word += line[i]
                            wordType = 'LEQ'
                        else:
                            wordType = 'LSS'
                    # 判断是'>'还是'>='
                    elif line[i] == '>':
                        if i+1<len(line) and line[i+1]=='=':
                            i += 1
                            word += line[i]
                            wordType = 'GEQ'
                        else:
                            wordType = 'GRE'
                    elif line[i] == ';':
                        wordType = 'SEMICN'
                    elif line[i] == ',':
                        wordType = 'COMMA'
                    elif line[i] == '{':
                        wordType = 'LBRACE'
                    elif line[i] == '}':
                        wordType = 'RBRACE'
                    elif line[i] == '[':
                        wordType = 'LBRACK'
                    elif line[i] == ']':
                        wordType = 'RBRACK'
                    elif line[i] == '(':
                        wordType = 'LPARENT'
                    elif line[i] == ')':
                        wordType = 'RPARENT'
                    # 判断字符串,即在双引号的内容
                    elif line[i] == '"':
                        word, i = '', i+1
                        while i<len(line) and (line[i]==' ' or line[i]=='!' or ord(line[i])>=35 and ord(line[i])<=126):
                            word += line[i]
                            i += 1
                        if i<len(line) and line[i]=='"':
                            wordType = 'STRCON'
                        else:
                            wordType = 'ILLEGAL'
                            word = line[i]
                    # 判断字符常量,即在单引号的内容
                    elif line[i] == '\'':
                            if i + 1 < len(line):
                                i += 1
                                if line[i] == '\'':
                                    word = ' '
                                    wordType = 'CHARCON'
                                elif line[i].isalnum() or line[i] == '_' or line[i] == '+' or line[i] == '-' or line[
                                    i] == '*' or line[i] == '/':
                                    word = line[i]
                                    wordType = 'CHARCON'
                                else:
                                    wordType = 'ILLEGAL'
                                i += 1
                            else:
                                wordType = 'ILLEGAL'
                    else:
                        wordType = 'ILLEGAL'
                fout.write(wordType+' '+word+'\n')
                i += 1